<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);
$tag_id = MiscUtils::getParam('tag', TAG_NEWS_ID);
$tags = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'art_tag', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d;

$SIZE = 20;
$page = MiscUtils::getParam('p', 1);
$order = MiscUtils::getParam('o', 't.date');
$queue = MiscUtils::getParam('q', 'DESC');

$tRoot = new stdClass();
$tRoot->id = 0;
$tRoot->name = 'ROOT';
$tRoot->parent_id = -1;
_createTagRoot($tags, $tRoot);

$rslt = new stdClass();
$rslt->tags = array();
_getTags($tRoot, $tag_id, $rslt, 0);
  
$cond_vals = NULL;
if ($tag_id != 0) {
  $cond_vals = new stdClass(); 
  $cond_vals->c = '';
  $cond_vals->v = array();
  foreach ($rslt->tags as $idx => &$t) {
    if ($cond_vals->c != '') {
      $cond_vals->c .= ' OR ';
    }
    $idx2 = $idx + 1;
    $cond_vals->c .= "t.tag_ids LIKE :v$idx2";
    $cond_vals->v[":v$idx2"] = "%|$t|%";
  }
}
$items = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'art_post', $cond_vals, $order, $queue, $page, $SIZE, NULL);

function _getTags($node, $tag_id, $rslt, $is_to_add) {
  if ($node->id == $tag_id || $is_to_add == 1) {
    $rslt->tags[] = $node->id;
  }
  foreach ($node->c as &$child) {
    _getTags($child, $tag_id, $rslt, (($is_to_add === 1) ? $is_to_add : $node->id == $tag_id));
  }
}
function _createTagRoot($arr, $node) {
  for ($i = 0, $il = count($arr); $i < $il; $i++) {
    $tmp = $arr[$i];
    if ($tmp->parent_id == $node->id) {
      $tmp->c = array();
      $node->c[] = $tmp;
      _createTagRoot($arr, $tmp);
    }
  }
}
function _showTagRoot($node) {
  global $page_id;
  if ($node->id != 0) {
    echo "<li><a href=\"#\" onclick=\"parent.location.href='http://" . URL . "admin.php?i=$page_id&tag=$node->id';\">$node->name</a>";
  }
  if (count($node->c) > 0 && $node->id != 0) {
    echo '<ul class="sub">';
  } else if ($node->id != 0) {
    echo '</li>';
  }
  foreach ($node->c as $child) {
    _showTagRoot($child);
  }
  if (count($node->c) > 0 && $node->id != 0) {
    echo '</ul></li>';
  }
}
function _strToTags($str) {
  $arr = explode('|', $str);
  array_splice($arr, 0, 1);
  array_splice($arr, count($arr) - 1, 1);
  $rslt = '';
  foreach ($arr as $idx => &$elm) {
    $rslt .= _idToTag($elm);
    if ($idx < count($arr) - 1) {
      $rslt .= ', ';
    }
  }
  return $rslt;
}
function _idToTag($id) {
  global $tags;
  foreach ($tags as &$tag) {
    if ($tag->id == $id) {
      return $tag->name;
    }
  }
  return '';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link href="css/dcverticalmegamenu.css" rel="stylesheet" type="text/css" />
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.list {
  border-left: 1px solid #666;
  border-top: 1px solid #666;
  font-size: 12px;
  width: 800px;
}
table.list th.sort {
  text-align: left;
  height: 28px;
  background-color: #222;
  padding: 0 5px; 
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  font-weight: bold;
  vertical-align: middle;
  cursor: pointer;
  color: #fff;
  overflow: hidden;
}
table.list tr.rowodd td {
  padding: 0 5px;
  height: 25px;
  background-color: #eee;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
  vertical-align: middle;
  overflow: hidden;
}
table.list tr.roweven td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.rowodd:hover td {
  background-color: #f4f3b2;
}
table.list tr.roweven:hover td {
  background-color: #f4f3b2;
}
table.list tr.none td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
}
.imgbtn {
  background-image: url(assets/imgs/add.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
}
.imgbtn:hover {
  background-color: #eee;
}
.imgico {
  cursor: pointer;
}
div.selected {
  line-height: 16px;
  background: #d1fbae;
  border: 1px solid #366b09;
  position: relative; /* Required */
  width: 250px;
  display: block;
  color: #333;
  padding: 8px 0 8px 25px;
  text-decoration: none;
  font-family: Verdana;
  text-align: left;
  display: inline-block;
}
.sub li {
  border-bottom: 1px solid #ddd;
  width: 100% !important;
  background: #666;
}
.sub li:hover {
  background: #aaa;
}
.sub a {
  font-family: Lucida Sans Unicode;
  margin-left: 20px;
}
</style>
</head>
<body>
<table cellpadding=0 cellspacing=0>
  <tr>
    <td style="vertical-align: top;">
      <ul id="mega-1" class="mega-menu">
        <?php _showTagRoot($tRoot); ?>
      </ul>
    </td>
    <td style="vertical-align: top; padding-left: 10px;">
      <button class="imgbtn" onclick="parent.location.href=parent.document.URL.replace('#', '') + '&script=index_post';">New Article</button>
      <div class="selected"><b><?php echo ($tag_id != 0) ? _idToTag($tag_id) : 'All tags'; ?></b></div>
      <div>
        <table class="list" style="margin-top: 5px;" cellPadding="0" cellSpacing="0">
          <tr>
            <th class="sort" onclick="set_order('title')">Subject</th>
            <th class="sort" width="200px" onclick="set_order('tag_ids')">Categories</th>
            <th class="sort" width="80px" onclick="set_order('date')">Date</th>
            <th class="sort" width="60px"></th>
          </tr>
          <?php foreach ($items->d as $idx => &$item) { ?>
          <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
            <td title="<?php echo $item->title; ?>"><?php echo MiscUtils::getShortName($item->title, 60); ?></td>
            <td title="<?php echo _strToTags($item->tag_ids); ?>"><?php echo MiscUtils::getShortName(_strToTags($item->tag_ids), 30); ?></td>
            <td><?php echo $item->date; ?></td>
            <td>
              <img src="assets/imgs/edit.png" class="imgico" onclick="parent.location.href=parent.document.URL.replace('#', '') + '&script=index_post&id=<?php echo $item->id; ?>';"></img>
              <img src="assets/imgs/delete24.png" class="imgico" onclick="del_tag('<?php echo $item->id; ?>');"></img>
            </td>
          </tr>
          <?php } ?>
          <?php if (count($items->d) == 0) { ?>
          <tr class="none" ><td colspan=4>No Results</td></tr>
          <?php } ?>
        </table>
        <div id="pagination"></div>
      </div>
    </td>
  </tr>
</table>

<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type='text/javascript' src='js/jquery.hoverIntent.minified.js'></script>
<script type='text/javascript' src='js/jquery.dcverticalmegamenu.1.3.js'></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../incls/js/pagination.js"></script>
<script>
function set_order(col) {
  parent.location.href='http://<?php echo URL. "admin.php?i=$page_id&tag=$tag_id&q=" . (($items->q == 'ASC') ? 'DESC' : 'ASC'); ?>&o=' +  col;
}

function del_tag(id) {
  r = window.confirm('Are You Sure?');
  if (!r) {
    return;
  }
  $.ajax({
    type: "POST",
    url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
    data: "action=del_tag&q=" + encodeURIComponent(id),
    success: function(msg){
      location.reload();
    }
  });
}

$(function(){
//  $('#mega-1').dcVerticalMegaMenu();
  
  var elm = document.getElementById('pagination');
  MyUtils.removeChildElements(elm);
  
  new ModulePagination(document, elm, null, null, null, null, {
    page: <?php echo $items->p; ?>,
    total: <?php echo $items->t; ?>,
    limit: <?php echo $items->s; ?>,
    callbackFunc: function(page) { 
      parent.location.href = 'http://<?php echo URL . "admin.php?i=$page_id&o=$items->o&q=$items->q"; ?>&p=' + page;
    },
    contenxt: this
  });
});
</script>
</body>
</html>
